using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class CueAndGuidelines : MonoBehaviour
{
	[Serializable]
	public class Line
	{
		[SerializeField]
		protected Vector3 offsetPositon;

		[SerializeField]
		protected Vector2 offsetSize = new Vector2(2f, 2f);

		[SerializeField]
		protected RectTransform line;

		[SerializeField]
		protected RectTransform outline;

		[SerializeField]
		protected RectTransform headPoint;

		[SerializeField]
		protected RectTransform tailPoint;

		[SerializeField]
		protected RectTransform headPoint2;

		[SerializeField]
		protected RectTransform tailPoint2;

		[SerializeField]
		public Image lineImage;

		[SerializeField]
		public Image outlineImage;

		[SerializeField]
		public Image headPointImage;

		[SerializeField]
		public Image tailPointImage;

		[SerializeField]
		public Image headPoint2Image;

		[SerializeField]
		public Image tailPoint2Image;

		public Transform parent => line.parent;

		public virtual Vector3 direction
		{
			get
			{
				return line.right;
			}
			set
			{
				line.right = value;
				outline.right = value;
				RefreshAttachPoint();
			}
		}

		public virtual Vector3 position
		{
			get
			{
				return line.position;
			}
			set
			{
				line.position = value;
				outline.position = line.localToWorldMatrix.MultiplyPoint(offsetPositon);
				RefreshAttachPoint();
			}
		}

		public virtual bool active
		{
			get
			{
				return line.gameObject.activeInHierarchy;
			}
			set
			{
				line.gameObject.SetActive(value);
				outline.gameObject.SetActive(value);
			}
		}

		public virtual Vector2 size
		{
			get
			{
				return line.sizeDelta;
			}
			set
			{
				line.sizeDelta = value;
				outline.sizeDelta = line.sizeDelta + offsetSize;
				RefreshAttachPoint();
			}
		}

		public virtual void Hide()
		{
			if ((bool)line)
			{
				line.gameObject.SetActive(value: false);
			}
			if ((bool)outline)
			{
				outline.gameObject.SetActive(value: false);
			}
			if ((bool)headPoint)
			{
				headPoint.gameObject.SetActive(value: false);
			}
			if ((bool)tailPoint)
			{
				tailPoint.gameObject.SetActive(value: false);
			}
			if ((bool)headPoint2)
			{
				headPoint2.gameObject.SetActive(value: false);
			}
			if ((bool)tailPoint2)
			{
				tailPoint2.gameObject.SetActive(value: false);
			}
		}

		public virtual void Show()
		{
			if ((bool)line)
			{
				line.gameObject.SetActive(value: true);
			}
			if ((bool)outline)
			{
				outline.gameObject.SetActive(value: true);
			}
			if ((bool)headPoint)
			{
				headPoint.gameObject.SetActive(value: true);
			}
			if ((bool)tailPoint)
			{
				tailPoint.gameObject.SetActive(value: true);
			}
			if ((bool)headPoint2)
			{
				headPoint2.gameObject.SetActive(value: true);
			}
			if ((bool)tailPoint2)
			{
				tailPoint2.gameObject.SetActive(value: true);
			}
		}

		public virtual void SetFade(float alpha)
		{
			SetFade(lineImage, alpha);
			SetFade(outlineImage, alpha);
			SetFade(headPointImage, alpha);
			SetFade(tailPointImage, alpha);
			SetFade(headPoint2Image, alpha);
			SetFade(tailPoint2Image, alpha);
		}

		protected void SetFade(Image image, float alpha)
		{
			if ((bool)image)
			{
				Color color = image.color;
				color.a = alpha;
				image.color = color;
			}
		}

		private void RefreshAttachPoint()
		{
			if ((bool)headPoint)
			{
				headPoint.position = line.position;
			}
			if ((bool)tailPoint)
			{
				RectTransform rectTransform = tailPoint;
				Matrix4x4 localToWorldMatrix = line.parent.localToWorldMatrix;
				Vector3 localPosition = line.localPosition;
				Vector2 sizeDelta = line.sizeDelta;
				rectTransform.position = localToWorldMatrix.MultiplyPoint(localPosition + sizeDelta.x * line.right);
			}
			if ((bool)headPoint2)
			{
				headPoint2.position = outline.position;
			}
			if ((bool)tailPoint2)
			{
				RectTransform rectTransform2 = tailPoint2;
				Matrix4x4 localToWorldMatrix2 = outline.parent.localToWorldMatrix;
				Vector3 localPosition2 = outline.localPosition;
				Vector2 sizeDelta2 = outline.sizeDelta;
				rectTransform2.position = localToWorldMatrix2.MultiplyPoint(localPosition2 + sizeDelta2.x * outline.right);
			}
		}
	}

	[Serializable]
	public class Circle : Line
	{
		[SerializeField]
		protected Vector2 offsetSize2 = new Vector2(-2f, -2f);

		[SerializeField]
		protected RectTransform outline2;

		[SerializeField]
		public Image outline2Image;

		public bool active
		{
			get
			{
				return base.active;
			}
			set
			{
				base.active = value;
				outline2.gameObject.SetActive(value);
			}
		}

		public Vector3 direction
		{
			get
			{
				return base.direction;
			}
			set
			{
				base.direction = value;
				outline2.right = value;
			}
		}

		public Vector3 position
		{
			get
			{
				return base.position;
			}
			set
			{
				base.position = value;
				outline2.position = outline.position;
			}
		}

		public Vector2 size
		{
			get
			{
				return base.size;
			}
			set
			{
				base.size = value;
				outline2.sizeDelta = line.sizeDelta + offsetSize2;
			}
		}

		public void Hide()
		{
			base.Hide();
			if ((bool)outline2)
			{
				outline2.gameObject.SetActive(value: false);
			}
		}

		public void Show()
		{
			base.Show();
			if ((bool)outline2)
			{
				outline2.gameObject.SetActive(value: true);
			}
		}

		public void SetFade(float alpha)
		{
			base.SetFade(alpha);
			SetFade(outline2Image, alpha);
		}
	}

	public Sprite[] cueList;

	private Vector3 m_Pivot;

	private float m_CueOffset;

	[SerializeField]
	[Range(0f, 2f)]
	private float m_CuePullStrength;

	[SerializeField]
	public Line m_Aim;

	[SerializeField]
	public Line m_CueBallDir;

	[SerializeField]
	public Line m_TargetBallDir;

	[SerializeField]
	public Circle m_CueBall;

	[SerializeField]
	private RectTransform m_Cue;

	[SerializeField]
	private RectTransform m_Forbidden;

	private Image m_ForbiddenImage;

	private bool m_bForbidden;

	[SerializeField]
	[Range(0f, 0.03f)]
	private float m_GuidelineOffset;

	private void Awake()
	{
		m_ForbiddenImage = m_Forbidden.GetComponent<Image>();
		m_Forbidden.gameObject.SetActive(value: false);
		base.gameObject.SetActive(value: false);
		m_Aim.direction = Vector3.left;
		FireSlider.OnSliderRelease = (Delegate0Args)Delegate.Combine(FireSlider.OnSliderRelease, new Delegate0Args(ReleaseCue));
		FireSlider.OnSliderValueChange = (Delegate1Args<float>)Delegate.Combine(FireSlider.OnSliderValueChange, new Delegate1Args<float>(AdjustingChange));
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Combine(PoolRulesBase.onNewTurn, new Delegate1Args<int>(RoundBegin));
		Image component = m_Cue.GetComponent<Image>();
		if (PlayerPrefs.HasKey("USE_CUE"))
		{
			int @int = PlayerPrefs.GetInt("USE_CUE");
			ItemInfo info = ItemInfoMgr.GetInfo(@int);
			if (info != null)
			{
				component.sprite = GetSpriteFromName(info.icon);
			}
		}
	}

	private Sprite GetSpriteFromName(string name)
	{
		for (int i = 0; i < cueList.Length; i++)
		{
			if (cueList[i].name.Equals(name))
			{
				return cueList[i];
			}
		}
		return null;
	}

	private void OnDestroy()
	{
		FireSlider.OnSliderRelease = (Delegate0Args)Delegate.Remove(FireSlider.OnSliderRelease, new Delegate0Args(ReleaseCue));
		FireSlider.OnSliderValueChange = (Delegate1Args<float>)Delegate.Remove(FireSlider.OnSliderValueChange, new Delegate1Args<float>(AdjustingChange));
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Remove(PoolRulesBase.onNewTurn, new Delegate1Args<int>(RoundBegin));
	}

	public void AdjustingChange(float value)
	{
		m_CueOffset = value;
		Pools.Cue.SetPowerScalar(value);
	}

	private void RoundBegin(int round)
	{
	}

	public void ReleaseCue()
	{
		StartCoroutine(Shoot());
	}

	private IEnumerator Shoot()
	{
		float time = 0.05f;
		iTween.ValueTo(m_Cue.gameObject, iTween.Hash("from", m_CueOffset, "to", 0, "time", time, "onupdate", "MotionBlur", "onupdatetarget", base.gameObject));
		yield return new WaitForSeconds(time + 0.01f);
		base.gameObject.SetActive(value: false);
		Pools.Cue.Fire();
	}

	private void MotionBlur(float value)
	{
		m_CueOffset = value;
	}

	public void SetFade(bool fade)
	{
		float num = 1f;
		if (fade)
		{
			num = 0.5f;
		}
		if ((bool)m_ForbiddenImage)
		{
			Color color = m_ForbiddenImage.color;
			color.a = num;
			m_ForbiddenImage.color = color;
		}
		m_Aim.SetFade(num);
		m_CueBallDir.SetFade(num);
		m_TargetBallDir.SetFade(num);
		m_CueBall.SetFade(num);
		Color color2 = m_Cue.GetComponent<Image>().color;
		color2.a = num;
		m_Cue.GetComponent<Image>().color = color2;
	}

	public void GuidePointerAt(Vector3 hitPoint, Transform hitTrans, Vector3 hitNormal, Vector3 fireDir)
	{
		Vector3 b = MathTools.World2UI(hitPoint);
		Vector3 vector = MathTools.World2UI(hitTrans.position);
		Vector3 vector2 = MathTools.World2UI(hitPoint + hitNormal.normalized * Pools.CueBall.GetRadius());
		Vector3 vector3 = m_Pivot = MathTools.World2UI(Pools.CueBall.GetPosition());
		m_CueBall.position = vector2;
		m_Forbidden.position = vector2;
		m_Aim.position = vector2 + fireDir * m_GuidelineOffset;
		Vector2 a = m_CueBall.parent.worldToLocalMatrix.MultiplyPoint(vector2);
		Vector2 b2 = m_CueBall.parent.worldToLocalMatrix.MultiplyPoint(vector3);
		Line aim = m_Aim;
		float x = Vector2.Distance(a, b2);
		Vector2 size = m_Aim.size;
		aim.size = new Vector2(x, size.y);
		m_Aim.direction = (vector3 - vector2).normalized;
		m_Cue.position = m_Pivot - m_Cue.right * m_CueOffset * m_CuePullStrength;
		m_Cue.right = -m_Aim.direction;
		if (hitTrans.CompareTag("Ball"))
		{
			m_TargetBallDir.active = !m_bForbidden;
			m_CueBallDir.active = !m_bForbidden;
			m_TargetBallDir.position = vector;
			Vector3 normalized = (vector - b).normalized;
			float num = Mathf.Max(Vector2.Dot(-m_Aim.direction, normalized), 0f);
			float num2 = num * (float)ConstantData.GuidelineLength;
			normalized *= num;
			m_TargetBallDir.direction = normalized;
			Line targetBallDir = m_TargetBallDir;
			float x2 = num2;
			Vector2 size2 = m_TargetBallDir.size;
			targetBallDir.size = new Vector2(x2, size2.y);
			m_CueBallDir.position = vector2;
			m_CueBallDir.direction = -m_Aim.direction - normalized;
			Line cueBallDir = m_CueBallDir;
			float x3 = (float)ConstantData.GuidelineLength - num2;
			Vector2 size3 = m_CueBallDir.size;
			cueBallDir.size = new Vector2(x3, size3.y);
		}
		else
		{
			m_TargetBallDir.active = false;
			m_CueBallDir.active = false;
		}
	}

	public Vector3 GetPointerDirection()
	{
		return -m_Aim.direction;
	}

	public void Forbidden()
	{
		m_bForbidden = true;
		m_TargetBallDir.active = !m_bForbidden;
		m_CueBallDir.active = !m_bForbidden;
		m_Forbidden.gameObject.SetActive(value: true);
		m_CueBall.active = false;
	}

	public void Allow()
	{
		m_bForbidden = false;
		m_Forbidden.gameObject.SetActive(value: false);
		m_CueBall.active = true;
	}
}
